package run.siyuan.rabbitmq.canal.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import run.siyuan.common.constant.rabbitmq.Constants;


@Configuration
public class RabbitConfig {

    /**
     * 延时交换机
     */
    @Bean
    public DirectExchange canalDelayExchange() {
        return new DirectExchange(Constants.Queue.RABBITMQ_SIYUAN_CANAL_EXCHANGE);
    }

    /**
     * 延时交换机队列
     */
    @Bean
    public Queue canalDelayExchangeQueue() {
        return new Queue(Constants.Queue.RABBITMQ_SIYUAN_CANAL_EXCHANGE_QUEUE, false, false, false);
    }

    /**
     * Fanout交换机和productQueue绑定
     */
    @Bean
    public Binding bindCanalExchangeAndQueue() {
        return BindingBuilder.bind(canalDelayExchangeQueue()).to(canalDelayExchange()).with(Constants.Queue.RABBITMQ_SIYUAN_CANAL_EXCHANGE_ROUTE_KEY);
    }

//    /**
//     * 配置消息的序列化
//     * bug: 不配置会将消息转换为byte数组
//     *
//     * @param connectionFactory
//     * @return
//     */
//    @Bean
//    public RabbitTemplate jsonRabbitTemplate(ConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return rabbitTemplate;
//    }
}